package com.chen.controller;

import com.chen.pojo.common.PageResult;
import com.chen.pojo.dto.SkuDto;
import com.chen.pojo.vo.SkuVo;
import com.chen.service.SkuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author chen
 * @version 1.0
 * @create_date 2025/8/17
 * 商品管理
 */
@Slf4j
@RestController
@RequestMapping("/api/vm-service/sku")
@Tag(name = "商品管理接口", description = "商品相关操作接口")
public class SkuController {

    @Autowired
    private SkuService service;

    //获取商圈下销量前10的商品(补货推荐)
    @GetMapping("/businessTop10/{businessId}")
    public List<SkuVo> getBusinessTop10(@PathVariable Integer businessId) {
        return service.getBusinessTop10(businessId);
    }

    @PostMapping("/fileUpload")
    @Operation(summary = "文件上传", description = "上传商品相关文件")
    public String fileUpload(@Parameter(description = "上传的文件") MultipartFile fileName) throws Exception {
        String result = service.fileUpload(fileName);
        log.info("上传成功：{}", result);
        return result;
    }

    /**
     * 商品搜索
     *
     * @param pageIndex 起始页
     * @param pageSize  页大小
     * @param skuName   商品名称
     * @return PageResult
     */
    @GetMapping("/search")
    @Operation(summary = "分页查询商品", description = "根据条件分页查询商品信息")
    public PageResult<SkuVo> searchList(
            @Parameter(description = "起始页") Integer pageIndex,
            @Parameter(description = "页大小") Integer pageSize,
            @Parameter(description = "商品名称") String skuName) {
        log.info("开始分页查询，当前页：{}，页面显示条数：{}，查询条件：商品名称-{}", pageIndex, pageSize, skuName);
        return service.searchList(pageIndex, pageSize, skuName);
    }

    /**
     * 新增商品
     *
     * @param skuDto 商品信息
     * @return 结果
     */
    @PostMapping
    @Operation(summary = "新增商品", description = "添加新的商品信息")
    public Boolean addSku(@Parameter(description = "商品信息") @RequestBody SkuDto skuDto) {
        log.info("新增商品操作，接收的参数：{}", skuDto);
        return service.addSku(skuDto);
    }

    /**
     * 更新
     *
     * @param skuId
     * @param skuDto
     * @return
     */
    @PutMapping("/{skuId}")
    @Operation(summary = "更新商品", description = "根据商品ID更新商品信息")
    public Boolean updateSku(@Parameter(description = "商品ID") @PathVariable String skuId,
                             @Parameter(description = "商品信息") @RequestBody SkuDto skuDto) {
        log.info("修改商品操作，商品id：{}，接收的参数：{}", skuId, skuDto);
        return service.updateSku(skuId, skuDto);
    }

}